package cn.iocoder.yudao.module.wrzj.controller.admin.wrzjuser;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wrzj.controller.admin.greatman.vo.UserGreatmanRespVO;
import cn.iocoder.yudao.module.wrzj.controller.admin.wrzjuser.vo.WrzjUserPageReqVO;
import cn.iocoder.yudao.module.wrzj.controller.admin.wrzjuser.vo.WrzjUserRespVO;
import cn.iocoder.yudao.module.wrzj.controller.admin.wrzjuser.vo.WrzjUserSaveReqVO;
import cn.iocoder.yudao.module.wrzj.dal.dataobject.wrzjuser.WrzjUserDO;
import cn.iocoder.yudao.module.wrzj.service.wrzjuser.WrzjUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 伟人之家-用户")
@RestController
@RequestMapping("/wrzj/user")
@Validated
public class WrzjUserController {

    @Resource
    private WrzjUserService userService;

//    @PostMapping("/create")
//    @Operation(summary = "创建伟人之家-用户")
//    @PreAuthorize("@ss.hasPermission('wrzj:user:create')")
//    public CommonResult<Long> createUser(@Valid @RequestBody WrzjUserSaveReqVO createReqVO) {
//        return success(userService.createUser(createReqVO));
//    }

    @PutMapping("/update")
    @Operation(summary = "更新伟人之家-用户")
    @PreAuthorize("@ss.hasPermission('wrzj:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody WrzjUserSaveReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除伟人之家-用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wrzj:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得伟人之家-用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wrzj:user:query')")
    public CommonResult<WrzjUserRespVO> getUser(@RequestParam("id") Long id) {
        WrzjUserDO user = userService.getUser(id);
        return success(BeanUtils.toBean(user, WrzjUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得伟人之家-用户分页")
    @PreAuthorize("@ss.hasPermission('wrzj:user:query')")
    public CommonResult<PageResult<WrzjUserRespVO>> getUserPage(@Valid WrzjUserPageReqVO pageReqVO) {
        PageResult<WrzjUserDO> pageResult = userService.getUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WrzjUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出伟人之家-用户 Excel")
    @PreAuthorize("@ss.hasPermission('wrzj:user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserExcel(@Valid WrzjUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WrzjUserDO> list = userService.getUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "伟人之家-用户.xls", "数据", WrzjUserRespVO.class,
                        BeanUtils.toBean(list, WrzjUserRespVO.class));
    }

    @GetMapping("/greatman-data")
    @Operation(summary = "获得用户伟人数据")
    @Parameter(name = "uid", description = "用户id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wrzj:user:query')")
    public CommonResult<List<UserGreatmanRespVO>> getUserGreatmanData(@RequestParam("uid") Long uid) {
        List<UserGreatmanRespVO> userGreatmanRespVOList = userService.getUserGreatmanData(uid);
        return success(userGreatmanRespVOList);
    }
}
